package com.websocket;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.converter.MessageConverter;
import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver;
import org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandler;
import org.springframework.messaging.simp.config.ChannelRegistration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketTransportRegistration;

import java.util.List;

@Configuration
/**
 * 注解EnableWebSocketMessageBroker开启使用stomp协议来传输基于代理的消息，这时控制器使用
 * @MessageMapping就像使用@RequestMapping
 */
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer{
	//注册stomp节点，并映射的指定url
	@Override
	public void registerStompEndpoints(StompEndpointRegistry registry) {
		//注册一个stomp的endpoing并指定使用sockjs协议
		registry.addEndpoint("/endpointWisely").withSockJS();
	}

	@Override
	public void configureWebSocketTransport(WebSocketTransportRegistration registry) {

	}

	@Override
	public void configureClientInboundChannel(ChannelRegistration registration) {

	}

	@Override
	public void configureClientOutboundChannel(ChannelRegistration registration) {

	}

	@Override
	public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {

	}

	@Override
	public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {

	}

	@Override
	public boolean configureMessageConverters(List<MessageConverter> messageConverters) {
		return false;
	}

	//配置消息代理
	@Override
	public void configureMessageBroker(MessageBrokerRegistry registry) {
		//广播式应配置一个/topic消息代理
		registry.enableSimpleBroker("/topic");
	}
}
